package dk.itu.mmad.travelapp4;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity {
	private String checkin = "";
	private String checkout = "";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	@Override
	protected void onStart(){
		super.onStart();
		final Button b1 = (Button) findViewById(R.id.b1);
		final Button b2 = (Button) findViewById(R.id.b2);
		b2.setEnabled(false);
		b1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText ed1 = (EditText) findViewById(R.id.text1);
				checkin = (String) ed1.getText().toString();
				if(checkin.equals("")){
					Toast.makeText(getApplicationContext(), "You have to type your origin first.", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(getApplicationContext(), "Origin: " + checkin, Toast.LENGTH_SHORT).show();
					b2.setEnabled(true);
					b1.setEnabled(false);
				}
			}
		});
		b2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText ed2 = (EditText) findViewById(R.id.text2);
				checkout = (String) ed2.getText().toString();
				if(checkin.equals("")){
					Toast.makeText(getApplicationContext(), "Enter your end stop", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(getApplicationContext(), "Destination: " + checkout, Toast.LENGTH_SHORT).show();
					EditText ed1 = (EditText) findViewById(R.id.text1);
					ed1.setText("");
					ed2.setText("");
					b2.setEnabled(false);
					b1.setEnabled(true);
					//Toast.makeText(getApplicationContext(), "Your travel is over", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putString("s1", checkin);
		outState.putString("s2", checkout);
	}
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		checkin = savedInstanceState.getString("s1");
		checkout = savedInstanceState.getString("s2");
	}
	MenuItem item;
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		item = menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "Receipt");
		item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		featureId = Menu.FIRST;
		item = this.item;
		Toast.makeText(getApplicationContext(), "You have traveled from " + checkin + " to " + checkout, Toast.LENGTH_LONG).show();
		return super.onMenuItemSelected(featureId, item);
	}
}
